<?php

namespace StarCloud\Library;

use Framework\DiServices\HttpClient;
use Phalcon\Di;

class Douyin
{
    use HttpCurl;
    protected $appId;
    protected $secret;
    protected $access_token;
    protected $httpClient;
    protected $cacheClass;

    const AUTH_URL = "https://developer.toutiao.com/api/apps/v2/token";

    const ADD_TASK_URL = "https://developer.toutiao.com/api/apps/taskbox/add_task";


    public function __construct($appId, $app_secret)
    {
        $this->appId = $appId;
        $this->secret = $app_secret;
        $this->cacheClass = Di::getDefault()->get('redis');
        $this->httpClient = Di::getDefault()->get('httpClient');
    }

    public function checkAuth($appid='', $appsecret='', $token='')
    {
        if (!$appid || !$appsecret) {
            $appid = $this->appId;
            $appsecret = $this->secret;
        }
        if ($token) { //手动指定token，优先使用
            $this->access_token=$token;
            return $this->access_token;
        }

        $authname = "douyin_" . $appid;
        var_dump($authname);
        if ($rs = $this->getCache($authname)) {
            $this->access_token = $rs;
            var_dump("cache" . $this->access_token);
            return $rs;
        }

        $body = [
            'appid' => $this->appId,
            'secret' => $this->secret,
            'grant_type'=> 'client_credential'
        ];

        $response = $this->httpClient->sendPostRequest(self::AUTH_URL, $body, ['Content-Type' => 'application/json']);
        $result = $response->getBody()->getContents();
        if ($result) {
            $json = json_decode($result, true);
            if ($json && isset($json['err_no']) && $json['err_no'] == 0) {
                $this->access_token = $json['data']['access_token'];
                $expire = $json['data']['expires_in'] ? intval($json['data']['expires_in'])-100 : 3600;
                $this->setCache($authname, $this->access_token, $expire);
                var_dump("get_access_token:" . $this->access_token);
                return $this->access_token;
            }
            return false;
        }
        return false;
    }

    public function createTask($taskName, $task_settle_type=1,$start_page, $task_icon, $task_tags)
    {
        if (!$this->checkAuth()) {
            var_dump("access error");
            return false;
        }

        $body = [
            'access_token' => $this->access_token,
            'appid' => $this->appId,
            'task_name' => $taskName,
            'task_settle_type' => $task_settle_type,
            'start_page' => $start_page,
            'anchor_title' => $taskName,
            'task_icon' => $task_icon,
            'task_start_time' => time(),
            'task_end_time' => time() + 90 * 86400,
            'task_desc' => '1.达人创作的视频内容需要与小程序内容相关联。2.不得有害品牌内容，欺骗用户。3.达人创作内容需积极向上,禁止出现低俗等违背社会公序良俗内容。',
            'refer_videos' => [
                "https://v.douyin.com/SK1QD14/"
            ],
            'refer_ma_captures' => [
                'https://cdn.doumvip.com/test1.png',
                'https://cdn.doumvip.com/test2.png',
                'https://cdn.doumvip.com/test3.png'
            ],
            'task_tags' => $task_tags,
        ];

        $response = $this->httpClient->sendPostRequest(self::ADD_TASK_URL, $body, ['Content-Type' => 'application/json']);
        $result = $response->getBody()->getContents();
        return json_decode($result, true);
    }
}